{template header}

<link href="{SITE_URL}public/bootstrap-datetimepicker/bootstrap-datetimepicker.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="{SITE_URL}public/bootstrap-datetimepicker/bootstrap-datetimepicker.js" charset="UTF-8"></script>
<script type="text/javascript" src="{SITE_URL}public/bootstrap-datetimepicker/bootstrap-datetimepicker.zh-CN.js" charset="UTF-8"></script>

<div class="container">


<div class="panel panel-default">
  <div class="panel-body">


<div class="row">
  <div class="col-md-2"></div>
  <div class="col-md-8">
  
  
<form method="POST" action="{SITE_URL}index.php?app=wish&ac=add&ts=do"  enctype="multipart/form-data" id="form1">

  <div class="form-group">
    <label>名称：</label>
    <input name="title" type="text" class="form-control">
  </div>
  
  
  {if $arrType}
  <div class="form-group">
    <label>类型：</label>

<select class="form-control" name="typeid">
{loop $arrType $key $item}
<option value="{$item[typeid]}">{$item[typename]}</option>
{/loop}
</select>
  </div>
  {/if}

  <div class="form-group">
    <label>封面：</label>
    <div class="">
       <img id="picshow" valign="middle" src="" style="height: 150px;">
       <div style="display: flex;">
         <input type="file" id="picfile" name="picfile">
         <div id="picsubmit" class="btn btn-success">上传照片</button> 
         <input type="hidden" name="photo" id="photo" />
       </div>
    </div>
  </div>

  <div class="form-group">
    <label>心愿详情{if $draft==''}<a class="btn btn-xs" href="{php echo(curPageURL())}&draft=1">恢复丢失内容</a>{/if}</label>
    <!-- <p>请至少插入一张图片，最后一张默认为封面</p> -->
    <div id="tseditor"></div>
    <div style="display: none;">
      <textarea id="content" name="content"></textarea>
    </div>
    <!--<textarea name="content" id="tseditor"></textarea>-->
  </div>

  <div class="form-group">
    <label>认领期限：</label>
    <p><input class="form-control" name="limittime" readonly value="" id="limitTime" /> </p>
  </div>


  <div class="form-group">
    <label>数目：</label>
    <p><input class="form-control" name="quota" value="" /> </p>
  </div>

  <div class="form-group">
    <label>购买链接：</label>
    <p><input class="form-control" name="link" value="" placeholder="可留空，仅供认领人参考"/> </p>
  </div>

  <div class="form-group">
    <label>联系人：</label>
    <p><input class="form-control" name="contact" value="" placeholder="不填则默认为学校联系人" /> </p>
  </div>

  <div class="form-group">
    <label>手机号：</label>
    <p><input class="form-control" name="phone" value=""  placeholder="不填则默认为学校联系人的电话" /> </p>
  </div>

  <div class="form-group">
    <label>愿主：</label>
    <p><input class="form-control" name="wisher" value="" /> </p>
  </div>


  <div class="form-group">
    <label>所属单位：</label>
    <p>
      <select  name="groupid" id="groupid" class="form-control">
        <option></option>
        {loop $arrGroup $key $item}
        <option value="{$item['groupid']}">{$item['groupname']}</option>
        {/loop}
      </select>
    </p>
  </div>

  <div class="form-group">
    <label>年级：</label>
    <p>

      <select  name="grade" id="grade" class="form-control">
        <option></option>
        <!--{for $i=1;$i<10;$i++}-->
        <option value="{$i}" >{php echo changeNum($i)}年级</option>
        <!--{/for}-->
      </select>
    </p>
  </div>

  <div class="form-group">
    <label>班：</label>
    <p>

      <select  name="class" id="class" class="form-control">
        <option></option>
        <!--{for $i=1;$i<20;$i++}-->
        <option value="{$i}" >{$i}班</option>
        <!--{/for}-->
      </select>
    </p>
  </div>







<div class="form-group">
  <input class="btn btn-success" type="submit" value="发布心愿">
</div>

  
</form>
  
  
  </div>
  <div class="col-md-2"></div>
</div>

</div>
</div>

</div>


<div id="protocol" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true"   style="overflow:scroll;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h5 class="modal-title">发布心愿须知</h5>
      </div>
      <div class="modal-body" >

        <div class="form-group">
          {$strTopic['content']}
        </div>

        <div class="form-group">
          <center><a class="btn btn-default" onclick="closeit()" >我知道了</a></center>
        </div>

      </div>
    </div>
  </div>
</div>

<script>
  $('#protocol').modal('show');
</script>

<script>
  function closeit(){
    $('#protocol').modal('hide');
  }
</script>

<script>
  $('#limitTime').datetimepicker({
    language:  'zh-CN',
    autoclose: true,
    format: 'yyyy-mm-dd hh:ii'
  });
</script>

<script src="{SITE_URL}public/js/image.js"></script>
<script>
  $(function(e){
    $('#form1').on('submit', function(e) {
      var title  = $('#title').val();
      var limittime  = $('#limitTime').val();
      var quota  = $('#quota').val();
      var content  = $('#content').val();
      var wisher = $('#wisher').val();
      var groupid = $('#groupid').val();
      var photo = $('#photo').val();


      if(title==''){
        bootbox.alert('名称必填');
        return false;
      }

      if(limittime==''){
        bootbox.alert('到期时间必填');
        return false;
      }

      if(quota==''){
        bootbox.alert('数量必填');
        return false;
      }



      if(wisher==''){
        bootbox.alert('愿主不能为空');
        return false;
      }

      if(groupid==''){
        bootbox.alert('所属单位不能为空');
        return false;
      }
      if(photo==''){
          bootbox.alert('必须上传封面');
          return false;
      }

      if (content == '<p><br></p>') {
        bootbox.alert('内容不能为空');
        return false;
      }

    });

    var picFile = null;
    // 封面上传监听
    $('#picfile').on("change", function (e) {
      $("#photo").val('')
      $("#picshow").attr('src', ''); 

      let file = e.target.files[0]
      compressImg(file, 0.25).then(res => {
        $("#picshow").attr('src', window.URL.createObjectURL(res.file)); 
        picFile = res.file
      })
    });
    
    var upload_url = "{SITE_URL}index.php?app=wish&ac=do&ts=photo";
    $('#picsubmit').on("click", function () {
      if (!picFile) {
        return bootbox.alert('请选择图片！');
      }
      var formData = new FormData();
      formData.append("file", picFile, picFile.filename);
      formData.append("upload_file", true);
      console.log(formData, picFile)
      $.ajax({
        url: upload_url,
        data: formData,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function(data){
          if (data != 0) {
            $("#photo").val(data)
            picFile = null
              return bootbox.alert('图片上传成功！');
          } else {
              return bootbox.alert('图片上传失败！');
          }
        }
      });
    });

  });

</script>


{php doAction('tseditor')}
{template footer}